package com.springcloud.sc.ucpv5.client.commondata.datachange;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;

public class DataExChangeUtil {

    private static boolean log = false;

    /**
     * 从corba对象转成prob对象
     * 
     * @param builder_src
     *            corba对象
     * @param withValue
     *            是否赋值
     * @return
     */
    public static Message dataChangeCommonToProBuf(DataExChange builder_src, boolean withValue) {
	try {
	    String className = builder_src.getClass().getName();

	    Message.Builder builder_dst = null;
	    Descriptors.Descriptor descriptor = null;
	    try {
		String pbClassName = "com.proto.uuos_sdk_pb$" + className.substring(className.lastIndexOf(".") + 1);
		Class cl = Class.forName(pbClassName); // 首先根据 pb message class
						       // 名称得到 Class
						       // 对象。（坑1：注意内部类连接符为$,如
						       // package_name.class_name$inner_class_name,
						       // 另外注意脚本启动的时候要注意对 $
						       // 转义，否则 shell 会以为
						       // $inner_class_name 为变量）
		Method method = cl.getMethod("newBuilder"); // newBuilder
							    // 为静态变量，即使没有
							    // message 的具体实例也可以
							    // invoke！yes！
		Object obj = method.invoke(null, null);
		builder_dst = (Message.Builder) obj; // 得到 builder
		descriptor = builder_dst.getDescriptorForType(); // 得到
								 // descriptor
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    if (builder_dst == null || descriptor == null) {
		return null;
	    }
	    if (withValue) {
		Field[] field = builder_src.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
		try {
		    for (int j = 0; j < field.length; j++) { // 遍历所有属性
			Field field_src = field[j];
			String name = field_src.getName(); // 获取属性的名字
			String type = field_src.getGenericType().toString(); // 获取属性的类型

			String type_proto = null;
			try {
			    if (name.equals("ParGrpTelno")) {
				System.out.println("");
			    }

			    String builder_name = "";
			    try {
				try {
				    // 1.首字母小写 2.存在"_"的采用去掉, 且首字母大写
				    String[] tmps = name.split("_");
				    StringBuilder builder = new StringBuilder();
				    for (int i = 0; i < tmps.length; i++) {
					builder.append(tmps[i].substring(0, 1).toLowerCase());
					builder.append(tmps[i].substring(1));
				    }
				    builder_name = builder.toString();
				} catch (Exception e) {
				    // TODO: handle exception
				}
			    } catch (Exception e) {
				// TODO: handle exception
			    }

			    Field field_proto = builder_dst.getClass().getDeclaredField(builder_name + "_");
			    if (field_proto != null) {
				type_proto = field_proto.getGenericType().toString(); // 获取属性的类型
			    }

			    // interface com.google.protobuf.LazyStringList
			    // class java.lang.String
			} catch (Exception e) {
			    // TODO: handle exception
			    e.printStackTrace();
			}

			// 取消属性的访问权限控制，即使private属性也可以进行访问。
			field_src.setAccessible(true);
			Object field_value = field_src.get(builder_src);

			if (field_value == null) {
			    continue;
			}

			// 构造set 方法的名称
			String builder_name = null;
			try {
			    // 1.首字母大写 2.存在"_"的采用去掉, 且首字母大写
			    String[] tmps = name.split("_");
			    StringBuilder builder = new StringBuilder();
			    for (int i = 0; i < tmps.length; i++) {
				builder.append(tmps[i].substring(0, 1).toUpperCase());
				builder.append(tmps[i].substring(1));
			    }
			    builder_name = builder.toString();
			} catch (Exception e) {
			    // TODO: handle exception
			}

			// System.out.println(builder_name);
			// System.out.println(type);
			Method m = null;
			if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class
								     // "，后面跟类名
			    if (type_proto != null && type_proto.indexOf("com.google.protobuf.LazyStringList") >= 0) {
				// 值为字符串数组
				List<String> tmpList = new ArrayList<String>();

				if (field_value != null) {
				    String[] tmps = field_value.toString().split("#");
				    if (tmps.length > 0) {
					for (String tmp : tmps) {
					    if (tmp != null && !"".equals(tmp)) {
						tmpList.add(tmp);
					    }
					}
				    }
				}
				field_value = tmpList;

				m = builder_dst.getClass().getMethod("add" + builder_name, String.class);
			    } else {
				m = builder_dst.getClass().getMethod("set" + builder_name, String.class);
				if (name.equals("SrcNodeNumber")) {
				    System.out.println("");
				}
				m.invoke(builder_dst, field_value);
			    }
			} else if (type.equals("int")) {
			    m = builder_dst.getClass().getMethod("set" + builder_name, int.class);
			    m.invoke(builder_dst, field_value);
			}
			// 属性名称:type 属性类型:short 相关类型没有处理!!!!!!!!!!
			else if (type.equals("short")) {
			    try {
				m = builder_dst.getClass().getMethod("set" + builder_name, int.class);
				m.invoke(builder_dst, field_value);
			    } catch (Exception e) {
				e.printStackTrace();
			    }
			} else if (type.equals("class java.lang.Integer")) {
			    m = builder_dst.getClass().getMethod("set" + builder_name, Integer.class);
			    m.invoke(builder_dst, field_value);
			} else if (type.equals("class java.lang.Boolean")) {
			    m = builder_dst.getClass().getMethod("set" + builder_name, Boolean.class);
			    m.invoke(builder_dst, field_value);
			} else if (type.equals("class java.util.Date")) {
			    m = builder_dst.getClass().getMethod("set" + builder_name, Date.class);
			    m.invoke(builder_dst, field_value);
			} else if (type.equals("class [B")) {
			    // byte 数组
			    m = builder_dst.getClass().getMethod("set" + builder_name, com.google.protobuf.ByteString.class);
			    // com.google.protobuf.ByteString value =
			    // com.google.protobuf.ByteString.EMPTY;
			    field_value = com.google.protobuf.ByteString.copyFrom((byte[]) field_value);
			} else if (type.indexOf("class [L") == 0) {
			    // 对象数组
			    int size = Array.getLength(field_value);
			    if (size == 0) {
				field_value = null;
			    }
			    List tmpValue = new ArrayList();
			    Message messageValue = null;
			    for (int i = 0; i < size; i++) {
				try {
				    messageValue = dataChangeCommonToProBuf((com.springcloud.sc.ucpv5.client.commondata.datachange.DataExChange) Array.get(field_value, i), true);
				    if (messageValue != null) {
					tmpValue.add(messageValue);
				    }
				} catch (Exception e) {
				    // TODO: handle exception
				}
			    }

			    if (messageValue != null) {
				m = builder_dst.getClass().getMethod("add" + builder_name, int.class, messageValue.getClass());
				// com.google.protobuf.ByteString value =
				// com.google.protobuf.ByteString.EMPTY;
				field_value = tmpValue;
			    }
			} else if (type.indexOf("com.springcloud.sc.ucpv5.client.commondata.data") == 0) {
			    field_value = dataChangeCommonToProBuf((com.springcloud.sc.ucpv5.client.commondata.datachange.DataExChange) field_value, true);
			    m = builder_dst.getClass().getMethod("set" + builder_name, field_value.getClass());
			} else {
			    System.out.println("属性名称:" + name + " 属性类型:" + type + "  相关类型没有处理!!!!!!!!!!");
			}

			try {
			    if (log)
				System.out.println("dataChangeToProBuf 赋值 :" + type + " " + name + "  " + field_value);
			} catch (Exception e) {
			    // TODO: handle exception
			}

			if (m != null && field_value != null) {
			    if (field_value instanceof List) {
				// 数组加入
				List tmpValue = (List) field_value;
				for (int i = 0; i < tmpValue.size(); i++) {
				    Object o = tmpValue.get(i);
				    if (o instanceof String) {
					m.invoke(builder_dst, o);
				    } else {
					m.invoke(builder_dst, i, o);
				    }
				}
			    } else {
				try {
				    m.invoke(builder_dst, field_value);
				} catch (Exception e) {
				    e.printStackTrace();
				}
			    }
			} else {
			    System.out.println("no mothen");
			}
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }

	    Message pbMessage = builder_dst.build();
	    return pbMessage;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }

    /**
     * 从corba对象转成prob对象
     * 
     * @param builder_src
     *            corba对象
     * @param withValue
     *            是否赋值
     * @return
     */
    public static org.omg.CORBA.portable.IDLEntity dataChangeCommonToCorbar(DataExChange builder_src, boolean withValue) {
	try {
	    String className = builder_src.getClass().getName();
	    org.omg.CORBA.portable.IDLEntity builder_dst = null;

	    try {
		String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.idl.sj3." + className.substring(className.lastIndexOf(".") + 1);
		Class cl = Class.forName(pbClassName); // 首先根据 pb message class
						       // 名称得到 Class
						       // 对象。（坑1：注意内部类连接符为$,如
						       // package_name.class_name$inner_class_name,
						       // 另外注意脚本启动的时候要注意对 $
						       // 转义，否则 shell 会以为

		builder_dst = (org.omg.CORBA.portable.IDLEntity) cl.newInstance();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    if (builder_dst == null) {
		return null;
	    }
	    if (withValue) {
		Field[] field = builder_src.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
		try {
		    for (int j = 0; j < field.length; j++) { // 遍历所有属性
			Field field_src = field[j];
			String name = field_src.getName(); // 获取属性的名字
			String type = field_src.getGenericType().toString(); // 获取属性的类型

			Field field_dst = null;
			try {
			    field_dst = builder_dst.getClass().getDeclaredField(name);
			} catch (Exception e) {
			    // TODO: handle exception
			    e.printStackTrace();
			}

			if (field_dst == null) {
			    System.out.println("没有找到对应字段:" + name);
			    continue;
			}

			// 取消属性的访问权限控制，即使private属性也可以进行访问。
			field_src.setAccessible(true);
			Object field_value = field_src.get(builder_src);

			if (field_value == null) {
			    System.out.println("没有找到对应字段:" + name);
			    continue;
			}
			if (type.indexOf("class com") == 0) {
			    DataExChange value_corbar = (DataExChange) field_value;
			    org.omg.CORBA.portable.IDLEntity messageValue = dataChangeCommonToCorbar(value_corbar, true);
			    field_value = messageValue;
			} else if (type.indexOf("class [L") == 0) {
			    DataExChange[] valueList_corbar = (DataExChange[]) field_value;
			    if (valueList_corbar.length == 0) {
				String name11 = type.replaceAll("class \\[L", "");
				name11 = name11.replaceAll(";", "");
				Object messageValue = dataChangeGetCommonDataClass(name11, false, true);
				field_value = Array.newInstance(messageValue.getClass(), 0);
			    } else {
				List tmpValue = new ArrayList();
				org.omg.CORBA.portable.IDLEntity messageValue = null;
				for (int i = 0; i < valueList_corbar.length; i++) {
				    try {
					Object o = valueList_corbar[i];
					messageValue = (org.omg.CORBA.portable.IDLEntity) dataChangeGetCommonDataClass(o.getClass().getName(), false, true);
					messageValue = dataChangeCommonToCorbar((DataExChange) o, true);
					if (messageValue != null) {
					    tmpValue.add(messageValue);
					}
				    } catch (Exception e) {
					// TODO: handle exception
				    }
				}

				Object newO = Array.newInstance(messageValue.getClass(), tmpValue.size());
				for (int i = 0; i < tmpValue.size(); i++) {
				    Array.set(newO, i, tmpValue.get(i));
				}

				field_value = newO;
			    }
			}

			if (field_value == null) {
			    System.out.println("没有找到对应字段:" + name);
			    continue;
			}
			field_dst.set(builder_dst, field_value);
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	    return builder_dst;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }

    /**
     * 从corba对象转成prob对象
     * 
     * @param builder_src
     *            corba对象
     * @param withValue
     *            是否赋值
     * @return
     */
    public static DataExChange dataChangeCorbaToCommon(DataExChange builder_dst, org.omg.CORBA.portable.IDLEntity builder_src, boolean withValue) {
	try {
	    String className = builder_src.getClass().getName();
	    if (builder_dst == null) {
		try {
		    String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.data." + className.substring(className.lastIndexOf(".") + 1);
		    Class cl = Class.forName(pbClassName); // 首先根据 pb message
							   // class 名称得到 Class
							   // 对象。（坑1：注意内部类连接符为$,如
							   // package_name.class_name$inner_class_name,
							   // 另外注意脚本启动的时候要注意对 $
							   // 转义，否则 shell 会以为
							   // $inner_class_name
							   // 为变量）
		    builder_dst = (DataExChange) cl.newInstance();
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	    if (withValue) {
		Field[] field = builder_src.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
		try {
		    for (int j = 0; j < field.length; j++) { // 遍历所有属性
			Field field_src = field[j];
			String name = field_src.getName(); // 获取属性的名字
			String type = field_src.getGenericType().toString(); // 获取属性的类型

			Field field_dst = null;
			try {
			    field_dst = builder_dst.getClass().getDeclaredField(name);
			} catch (Exception e) {
			    // TODO: handle exception
			    e.printStackTrace();
			}

			if (field_dst == null) {
			    System.out.println("没有找到对应字段:" + name);
			    continue;
			}

			// 取消属性的访问权限控制，即使private属性也可以进行访问。
			field_src.setAccessible(true);
			Object field_value = field_src.get(builder_src);

			if (field_value == null) {
			    System.out.println("没有找到对应字段:" + name);
			    continue;
			}
			if (type.indexOf("class com") == 0) {
			    org.omg.CORBA.portable.IDLEntity value_corbar = (org.omg.CORBA.portable.IDLEntity) field_value;
			    DataExChange messageValue = null;
			    messageValue = (DataExChange) dataChangeGetCommonDataClass(value_corbar.getClass().getName(), false, false);
			    messageValue.dataChangeCorbarDataToCommon(value_corbar);
			    field_value = messageValue;
			} else if (type.indexOf("class [L") == 0) {
			    org.omg.CORBA.portable.IDLEntity[] valueList_corbar = (org.omg.CORBA.portable.IDLEntity[]) field_value;
			    if (valueList_corbar.length == 0) {
				Object oldValue = field_dst.get(builder_dst);
				if (oldValue != null && Array.getLength(oldValue) > 0) {
				    // 原来已经包含,则不需要做修改
				    field_value = oldValue;
				} else {
				    String name11 = type.replaceAll("class \\[L", "");
				    name11 = name11.replaceAll(";", "");
				    DataExChange messageValue = (DataExChange) dataChangeGetCommonDataClass(name11, false, false);
				    ;
				    field_value = Array.newInstance(messageValue.getClass(), 0);
				}
			    } else {
				List tmpValue = new ArrayList();
				// 从旧的地方,添加进来新的对象
				try {
				    Object oldValue = field_dst.get(builder_dst);
				    if (oldValue != null && Array.getLength(oldValue) > 0) {
					int oldValueSize = Array.getLength(oldValue);
					for (int i = 0; i < Array.getLength(oldValue); i++) {
					    tmpValue.add(Array.get(oldValue, i));
					}
				    }
				} catch (Exception e) {
				    e.printStackTrace();
				}

				DataExChange messageValue = null;
				for (int i = 0; i < valueList_corbar.length; i++) {
				    try {
					Object o = valueList_corbar[i];
					messageValue = (DataExChange) dataChangeGetCommonDataClass(o.getClass().getName(), false, false);
					messageValue.dataChangeCorbarDataToCommon((org.omg.CORBA.portable.IDLEntity) o);
					if (messageValue != null) {
					    tmpValue.add(messageValue);
					}
				    } catch (Exception e) {
					// TODO: handle exception
				    }
				}

				Object newO = Array.newInstance(messageValue.getClass(), tmpValue.size());
				for (int i = 0; i < tmpValue.size(); i++) {
				    Array.set(newO, i, tmpValue.get(i));
				}

				field_value = newO;
			    }
			}

			if (field_value != null) {
			    try {
				field_dst.set(builder_dst, field_value);
			    } catch (Exception e) {
				System.out.println("赋值异常:" + type + " " + name);
				e.printStackTrace();
			    }
			}
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	    return builder_dst;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }

    /**
     * 从probu转成corba对象
     * 
     * @param builder_src
     * @param withValue
     * @return
     */
    public static DataExChange dataChangeProBufToCommon(DataExChange builder_dst, Message builder_src, boolean withValue) {
	try {
	    String className = builder_src.getClass().getName();
	    if (builder_dst == null) {
		try {
		    String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.data." + className.substring(className.lastIndexOf("$") + 1);
		    Class cl = Class.forName(pbClassName); // 首先根据 pb message
							   // class 名称得到 Class
							   // 对象。（坑1：注意内部类连接符为$,如
							   // package_name.class_name$inner_class_name,
							   // 另外注意脚本启动的时候要注意对 $
							   // 转义，否则 shell 会以为
							   // $inner_class_name
							   // 为变量）
		    builder_dst = (DataExChange) cl.newInstance();
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }

	    if (withValue) {
		Field[] field = builder_dst.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
		try {
		    for (int j = 0; j < field.length; j++) { // 遍历所有属性
			Field field_dst = field[j];
			String name = field_dst.getName(); // 获取属性的名字
			String type = field_dst.getGenericType().toString(); // 获取属性的类型

			String type_proto = null;
			try {
			    if (name.equals("ParGrpTelno")) {
				System.out.println("");
			    }
			    String builder_name = "";
			    try {
				try {
				    // 1.首字母小写 2.存在"_"的采用去掉, 且首字母大写
				    String[] tmps = name.split("_");
				    StringBuilder builder = new StringBuilder();
				    for (int i = 0; i < tmps.length; i++) {
					if (i > 0) {
					    builder.append(tmps[i].substring(0, 1).toUpperCase());
					} else {
					    builder.append(tmps[i].substring(0, 1).toLowerCase());
					}
					builder.append(tmps[i].substring(1));

				    }
				    builder_name = builder.toString();
				} catch (Exception e) {
				    e.printStackTrace();
				}
			    } catch (Exception e) {
				e.printStackTrace();
			    }

			    try {
				Field field_proto = builder_src.getClass().getDeclaredField(builder_name + "_");
				if (field_proto != null) {
				    type_proto = field_proto.getGenericType().toString(); // 获取属性的类型
				}
			    } catch (Exception e) {
				e.printStackTrace();
			    }

			    // interface com.google.protobuf.LazyStringList
			    // class java.lang.String
			} catch (Exception e) {
			    // TODO: handle exception
			    e.printStackTrace();
			}

			// 取消属性的访问权限控制，即使private属性也可以进行访问。
			field_dst.setAccessible(true);

			// set 方法// 构造set 方法的名称
			String builder_name = null;
			try {
			    // 1.首字母大写 2.存在"_"的采用去掉, 且首字母大写
			    String[] tmps = name.split("_");
			    StringBuilder builder = new StringBuilder();
			    for (int i = 0; i < tmps.length; i++) {
				builder.append(tmps[i].substring(0, 1).toUpperCase());
				builder.append(tmps[i].substring(1));
			    }
			    builder_name = builder.toString();
			} catch (Exception e) {
			    e.printStackTrace();
			}
			Method m = null;
			Object field_value = null;
			if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class
								     // "，后面跟类名
			    if (type_proto != null && type_proto.indexOf("com.google.protobuf.LazyStringList") >= 0) {
				m = builder_src.getClass().getMethod("get" + builder_name + "List");

				field_value = m.invoke(builder_src);

				StringBuilder builder = new StringBuilder();
				if (field_value != null && field_value instanceof com.google.protobuf.ProtocolStringList) {
				    com.google.protobuf.ProtocolStringList file_value_ = (com.google.protobuf.ProtocolStringList) field_value;
				    for (int i = 0; i < file_value_.size(); i++) {
					String tmp = file_value_.get(i);
					if (tmp != null && !"".equals(tmp)) {
					    builder.append(tmp);
					}
					if (i < (file_value_.size() - 1)) {
					    builder.append("#");
					}
				    }
				}
				field_value = builder.toString();
			    } else {
				m = builder_src.getClass().getMethod("get" + builder_name);
				field_value = m.invoke(builder_src);
			    }
			} else if (type.equals("int")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    field_value = m.invoke(builder_src);
			} else if (type.equals("short")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    field_value = m.invoke(builder_src);
			} else if (type.equals("class java.lang.Integer")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    field_value = m.invoke(builder_src);
			} else if (type.equals("class java.lang.Boolean")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    field_value = m.invoke(builder_src);
			} else if (type.equals("class java.util.Date")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    field_value = m.invoke(builder_src);
			} else if (type.equals("class [B")) {
			    m = builder_src.getClass().getMethod("get" + builder_name);
			    // com.google.protobuf.ByteString value =
			    // com.google.protobuf.ByteString.EMPTY;
			    field_value = m.invoke(builder_src);

			    field_value = ((com.google.protobuf.ByteString) field_value).toByteArray();
			} else if (type.indexOf("class [L") == 0) {
			    // 对象数组
			    m = builder_src.getClass().getMethod("get" + builder_name + "List");

			    field_value = m.invoke(builder_src);

			    List valueList = (List) field_value;
			    if (valueList.size() == 0) {
				// String className = type.replaceAll("class
				// [L", "");
				// Class cl = Class.forName(pbClassName);
				// com.springcloud.sc.ucpv5.client.idl.sj3.Group
				String name11 = type.replaceAll("class \\[L", "");
				name11 = name11.replaceAll(";", "");
				field_value = Array.newInstance(Class.forName(name11), 0);
			    } else {
				List tmpValue = new ArrayList();
				DataExChange messageValue = null;
				for (Object obj : valueList) {
				    try {
					messageValue = (DataExChange) dataChangeGetCommonDataClass(obj.getClass().getName(), true, false);
					messageValue.dataChangeProBufDataToCommon((Message) obj);
					tmpValue.add(messageValue);
				    } catch (Exception e) {
					// TODO: handle exception
				    }
				}

				Object newO = Array.newInstance(messageValue.getClass(), tmpValue.size());
				for (int i = 0; i < tmpValue.size(); i++) {
				    Array.set(newO, i, tmpValue.get(i));
				}

				field_value = newO;
			    }
			} else if (type.indexOf("class com.springcloud.sc.ucpv5.client.commondata.data.") == 0) {
			    try {
				m = builder_src.getClass().getMethod("get" + builder_name);
				field_value = m.invoke(builder_src);
				// 转成corba对象
				DataExChange messageValue = (DataExChange) dataChangeGetCommonDataClass(field_value.getClass().getName(), true, false);
				messageValue.dataChangeProBufDataToCommon((Message) field_value);
				field_value = messageValue;
			    } catch (Exception e) {
				e.printStackTrace();
			    }
			} else {
			    System.out.println(type + "相关类型没有处理!!!!!!!!!!");
			}

			try {
			    if (log)
				System.out.println("dataChangeToCorba 赋值 :" + type + " " + name + "  " + field_value);
			} catch (Exception e) {
			    // TODO: handle exception
			}

			if (field_value != null) {
			    field_dst.set(builder_dst, field_value);
			} else {
			    System.out.println("no value");
			}
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	    return builder_dst;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }

    public static Message.Builder dataChangeGetProBufBuilderClass(String className) {
	try {
	    Message.Builder builder_dst = null;
	    Descriptors.Descriptor descriptor = null;
	    try {
		String pbClassName = "com.proto.uuos_sdk_pb$" + className.substring(className.lastIndexOf(".") + 1);
		Class cl = Class.forName(pbClassName); // 首先根据 pb message class
						       // 名称得到 Class
						       // 对象。（坑1：注意内部类连接符为$,如
						       // package_name.class_name$inner_class_name,
						       // 另外注意脚本启动的时候要注意对 $
						       // 转义，否则 shell 会以为
						       // $inner_class_name 为变量）
		Method method = cl.getMethod("newBuilder"); // newBuilder
							    // 为静态变量，即使没有
							    // message 的具体实例也可以
							    // invoke！yes！
		Object obj = method.invoke(null, null);
		builder_dst = (Message.Builder) obj; // 得到 builder
		descriptor = builder_dst.getDescriptorForType(); // 得到
								 // descriptor
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    if (builder_dst == null || descriptor == null) {
		return null;
	    }
	    return builder_dst;
	} catch (Exception e) {
	    // TODO: handle exception
	}
	return null;
    }

    public static Object dataChangeGetCommonDataClass(String className, boolean isProBuf, boolean isCorba) {
	Object builder_dst = null;
	try {
	    String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.data." + className.substring(className.lastIndexOf(".") + 1);
	    if (isProBuf) {
		pbClassName = "com.springcloud.sc.ucpv5.client.commondata.data." + className.substring(className.lastIndexOf("$") + 1);
	    }
	    if (isCorba) {
		pbClassName = "com.springcloud.sc.ucpv5.client.commondata.idl.sj3." + className.substring(className.lastIndexOf(".") + 1);
	    }
	    Class cl = Class.forName(pbClassName); // 首先根据 pb message
						   // class 名称得到 Class
						   // 对象。（坑1：注意内部类连接符为$,如
						   // package_name.class_name$inner_class_name,
						   // 另外注意脚本启动的时候要注意对 $
						   // 转义，否则 shell 会以为
						   // $inner_class_name
						   // 为变量）
	    builder_dst = cl.newInstance();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return builder_dst;
    }

    public static org.omg.CORBA.portable.Streamable dataChangeGetCorbaHolder(org.omg.CORBA.portable.IDLEntity entity) {
	try {
	    String className = entity.getClass().getName();
	    org.omg.CORBA.portable.Streamable builder_dst = null;
	    try {
		String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.idl.sj3." + className.substring(className.lastIndexOf(".") + 1) + "Holder";
		Class cl = Class.forName(pbClassName); // 首先根据 pb message class
						       // 名称得到 Class
						       // 对象。（坑1：注意内部类连接符为$,如
						       // package_name.class_name$inner_class_name,
						       // 另外注意脚本启动的时候要注意对 $
						       // 转义，否则 shell 会以为
						       // $inner_class_name 为变量）

		Constructor c = cl.getConstructor(entity.getClass());// 获取有参构造

		builder_dst = (org.omg.CORBA.portable.Streamable) c.newInstance(entity); // 通过有参构造创建对象
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    return builder_dst;
	} catch (Exception e) {
	    // TODO: handle exception
	}
	return null;
    }

    public static org.omg.CORBA.portable.Streamable dataChangeGetCorbaHolder(String className) {
	try {
	    org.omg.CORBA.portable.Streamable builder_dst = null;
	    try {
		String pbClassName = "com.springcloud.sc.ucpv5.client.commondata.idl.sj3." + className.substring(className.lastIndexOf(".") + 1) + "Holder";
		Class cl = Class.forName(pbClassName); // 首先根据 pb message class
						       // 名称得到 Class
						       // 对象。（坑1：注意内部类连接符为$,如
						       // package_name.class_name$inner_class_name,
						       // 另外注意脚本启动的时候要注意对 $
						       // 转义，否则 shell 会以为
						       // $inner_class_name 为变量）
		builder_dst = (org.omg.CORBA.portable.Streamable) cl.newInstance();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    return builder_dst;
	} catch (Exception e) {
	    // TODO: handle exception
	}
	return null;
    }
}
